﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.Localization;

namespace Terraria.ModLoader
{
	// Token: 0x0200017B RID: 379
	public static class BuffLoader
	{
		// Token: 0x06000F3D RID: 3901 RVA: 0x00401E24 File Offset: 0x00400024
		static BuffLoader()
		{
			for (int i = 0; i < 206; i++)
			{
				BuffLoader.vanillaCanBeCleared[i] = true;
			}
			BuffLoader.vanillaLongerExpertDebuff[20] = true;
			BuffLoader.vanillaLongerExpertDebuff[22] = true;
			BuffLoader.vanillaLongerExpertDebuff[23] = true;
			BuffLoader.vanillaLongerExpertDebuff[24] = true;
			BuffLoader.vanillaLongerExpertDebuff[30] = true;
			BuffLoader.vanillaLongerExpertDebuff[31] = true;
			BuffLoader.vanillaLongerExpertDebuff[32] = true;
			BuffLoader.vanillaLongerExpertDebuff[33] = true;
			BuffLoader.vanillaLongerExpertDebuff[35] = true;
			BuffLoader.vanillaLongerExpertDebuff[36] = true;
			BuffLoader.vanillaLongerExpertDebuff[39] = true;
			BuffLoader.vanillaLongerExpertDebuff[44] = true;
			BuffLoader.vanillaLongerExpertDebuff[46] = true;
			BuffLoader.vanillaLongerExpertDebuff[47] = true;
			BuffLoader.vanillaLongerExpertDebuff[69] = true;
			BuffLoader.vanillaLongerExpertDebuff[70] = true;
			BuffLoader.vanillaLongerExpertDebuff[80] = true;
			BuffLoader.vanillaCanBeCleared[21] = false;
			BuffLoader.vanillaCanBeCleared[28] = false;
			BuffLoader.vanillaCanBeCleared[34] = false;
			BuffLoader.vanillaCanBeCleared[86] = false;
			BuffLoader.vanillaCanBeCleared[87] = false;
			BuffLoader.vanillaCanBeCleared[89] = false;
			BuffLoader.vanillaCanBeCleared[199] = false;
		}

		// Token: 0x06000F3E RID: 3902 RVA: 0x00401F60 File Offset: 0x00400160
		internal static int ReserveBuffID()
		{
			if (ModNet.AllowVanillaClients)
			{
				throw new Exception("Adding buffs breaks vanilla client compatibility");
			}
			int result = BuffLoader.nextBuff;
			BuffLoader.nextBuff++;
			return result;
		}

		// Token: 0x1700011E RID: 286
		// (get) Token: 0x06000F3F RID: 3903 RVA: 0x0000E628 File Offset: 0x0000C828
		public static int BuffCount
		{
			get
			{
				return BuffLoader.nextBuff;
			}
		}

		// Token: 0x06000F40 RID: 3904 RVA: 0x0000E62F File Offset: 0x0000C82F
		public static ModBuff GetBuff(int type)
		{
			if (type < 206 || type >= BuffLoader.BuffCount)
			{
				return null;
			}
			return BuffLoader.buffs[type - 206];
		}

		// Token: 0x06000F41 RID: 3905 RVA: 0x00401F94 File Offset: 0x00400194
		internal unsafe static void ResizeArrays()
		{
			Array.Resize<bool>(ref Main.pvpBuff, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.persistentBuff, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.vanityPet, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.lightPet, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.meleeBuff, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.debuff, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.buffNoSave, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.buffNoTimeDisplay, BuffLoader.nextBuff);
			Array.Resize<bool>(ref Main.buffDoubleApply, BuffLoader.nextBuff);
			Array.Resize<float>(ref Main.buffAlpha, BuffLoader.nextBuff);
			Array.Resize<Texture2D>(ref Main.buffTexture, BuffLoader.nextBuff);
			Array.Resize<LocalizedText>(ref Lang._buffNameCache, BuffLoader.nextBuff);
			Array.Resize<LocalizedText>(ref Lang._buffDescriptionCache, BuffLoader.nextBuff);
			for (int i = 206; i < BuffLoader.nextBuff; i++)
			{
				Lang._buffNameCache[i] = LocalizedText.Empty;
				Lang._buffDescriptionCache[i] = LocalizedText.Empty;
			}
			ModLoader.BuildGlobalHook<GlobalBuff, BuffLoader.DelegateUpdatePlayer>(ref BuffLoader.HookUpdatePlayer, BuffLoader.globalBuffs, (GlobalBuff g) => (BuffLoader.DelegateUpdatePlayer)Delegate.CreateDelegate(typeof(BuffLoader.DelegateUpdatePlayer), g, methodof(GlobalBuff.Update(int, Player, int*))));
			ModLoader.BuildGlobalHook<GlobalBuff, BuffLoader.DelegateUpdateNPC>(ref BuffLoader.HookUpdateNPC, BuffLoader.globalBuffs, (GlobalBuff g) => (BuffLoader.DelegateUpdateNPC)Delegate.CreateDelegate(typeof(BuffLoader.DelegateUpdateNPC), g, methodof(GlobalBuff.Update(int, NPC, int*))));
			ModLoader.BuildGlobalHook<GlobalBuff, Func<int, Player, int, int, bool>>(ref BuffLoader.HookReApplyPlayer, BuffLoader.globalBuffs, (GlobalBuff g) => (Func<int, Player, int, int, bool>)Delegate.CreateDelegate(typeof(Func<int, Player, int, int, bool>), g, methodof(GlobalBuff.ReApply(int, Player, int, int))));
			ModLoader.BuildGlobalHook<GlobalBuff, Func<int, NPC, int, int, bool>>(ref BuffLoader.HookReApplyNPC, BuffLoader.globalBuffs, (GlobalBuff g) => (Func<int, NPC, int, int, bool>)Delegate.CreateDelegate(typeof(Func<int, NPC, int, int, bool>), g, methodof(GlobalBuff.ReApply(int, NPC, int, int))));
			ModLoader.BuildGlobalHook<GlobalBuff, BuffLoader.DelegateModifyBuffTip>(ref BuffLoader.HookModifyBuffTip, BuffLoader.globalBuffs, (GlobalBuff g) => (BuffLoader.DelegateModifyBuffTip)Delegate.CreateDelegate(typeof(BuffLoader.DelegateModifyBuffTip), g, methodof(GlobalBuff.ModifyBuffTip(int, string*, int*))));
			ModLoader.BuildGlobalHook<GlobalBuff, Action<string, List<Vector2>>>(ref BuffLoader.HookCustomBuffTipSize, BuffLoader.globalBuffs, (GlobalBuff g) => (Action<string, List<Vector2>>)Delegate.CreateDelegate(typeof(Action<string, List<Vector2>>), g, methodof(GlobalBuff.CustomBuffTipSize(string, List<Vector2>))));
			ModLoader.BuildGlobalHook<GlobalBuff, Action<string, SpriteBatch, int, int>>(ref BuffLoader.HookDrawCustomBuffTip, BuffLoader.globalBuffs, (GlobalBuff g) => (Action<string, SpriteBatch, int, int>)Delegate.CreateDelegate(typeof(Action<string, SpriteBatch, int, int>), g, methodof(GlobalBuff.DrawCustomBuffTip(string, SpriteBatch, int, int))));
		}

		// Token: 0x06000F42 RID: 3906 RVA: 0x0000E654 File Offset: 0x0000C854
		internal static void Unload()
		{
			BuffLoader.buffs.Clear();
			BuffLoader.nextBuff = 206;
			BuffLoader.globalBuffs.Clear();
		}

		// Token: 0x06000F43 RID: 3907 RVA: 0x0000E674 File Offset: 0x0000C874
		internal static bool IsModBuff(int type)
		{
			return type >= 206;
		}

		// Token: 0x06000F44 RID: 3908 RVA: 0x004024E4 File Offset: 0x004006E4
		public static void Update(int buff, Player player, ref int buffIndex)
		{
			int num = buffIndex;
			if (BuffLoader.IsModBuff(buff))
			{
				BuffLoader.GetBuff(buff).Update(player, ref buffIndex);
			}
			foreach (BuffLoader.DelegateUpdatePlayer delegateUpdatePlayer in BuffLoader.HookUpdatePlayer)
			{
				if (buffIndex != num)
				{
					break;
				}
				delegateUpdatePlayer(buff, player, ref buffIndex);
			}
		}

		// Token: 0x06000F45 RID: 3909 RVA: 0x00402530 File Offset: 0x00400730
		public static void Update(int buff, NPC npc, ref int buffIndex)
		{
			if (BuffLoader.IsModBuff(buff))
			{
				BuffLoader.GetBuff(buff).Update(npc, ref buffIndex);
			}
			foreach (BuffLoader.DelegateUpdateNPC delegateUpdateNPC in BuffLoader.HookUpdateNPC)
			{
				delegateUpdateNPC(buff, npc, ref buffIndex);
			}
		}

		// Token: 0x06000F46 RID: 3910 RVA: 0x00402574 File Offset: 0x00400774
		public static bool ReApply(int buff, Player player, int time, int buffIndex)
		{
			foreach (Func<int, Player, int, int, bool> func in BuffLoader.HookReApplyPlayer)
			{
				if (func(buff, player, time, buffIndex))
				{
					return true;
				}
			}
			return BuffLoader.IsModBuff(buff) && BuffLoader.GetBuff(buff).ReApply(player, time, buffIndex);
		}

		// Token: 0x06000F47 RID: 3911 RVA: 0x004025C0 File Offset: 0x004007C0
		public static bool ReApply(int buff, NPC npc, int time, int buffIndex)
		{
			foreach (Func<int, NPC, int, int, bool> func in BuffLoader.HookReApplyNPC)
			{
				if (func(buff, npc, time, buffIndex))
				{
					return true;
				}
			}
			return BuffLoader.IsModBuff(buff) && BuffLoader.GetBuff(buff).ReApply(npc, time, buffIndex);
		}

		// Token: 0x06000F48 RID: 3912 RVA: 0x0000E681 File Offset: 0x0000C881
		public static bool LongerExpertDebuff(int buff)
		{
			ModBuff buff2 = BuffLoader.GetBuff(buff);
			if (buff2 == null)
			{
				return BuffLoader.vanillaLongerExpertDebuff[buff];
			}
			return buff2.longerExpertDebuff;
		}

		// Token: 0x06000F49 RID: 3913 RVA: 0x0000E69A File Offset: 0x0000C89A
		public static bool CanBeCleared(int buff)
		{
			ModBuff buff2 = BuffLoader.GetBuff(buff);
			if (buff2 == null)
			{
				return BuffLoader.vanillaCanBeCleared[buff];
			}
			return buff2.canBeCleared;
		}

		// Token: 0x06000F4A RID: 3914 RVA: 0x0040260C File Offset: 0x0040080C
		public static void ModifyBuffTip(int buff, ref string tip, ref int rare)
		{
			if (BuffLoader.IsModBuff(buff))
			{
				BuffLoader.GetBuff(buff).ModifyBuffTip(ref tip, ref rare);
			}
			foreach (BuffLoader.DelegateModifyBuffTip delegateModifyBuffTip in BuffLoader.HookModifyBuffTip)
			{
				delegateModifyBuffTip(buff, ref tip, ref rare);
			}
		}

		// Token: 0x06000F4B RID: 3915 RVA: 0x00402650 File Offset: 0x00400850
		public static void CustomBuffTipSize(string buffTip, List<Vector2> sizes)
		{
			foreach (Action<string, List<Vector2>> action in BuffLoader.HookCustomBuffTipSize)
			{
				action(buffTip, sizes);
			}
		}

		// Token: 0x06000F4C RID: 3916 RVA: 0x00402680 File Offset: 0x00400880
		public static void DrawCustomBuffTip(string buffTip, SpriteBatch spriteBatch, int originX, int originY)
		{
			foreach (Action<string, SpriteBatch, int, int> action in BuffLoader.HookDrawCustomBuffTip)
			{
				action(buffTip, spriteBatch, originX, originY);
			}
		}

		// Token: 0x040012B5 RID: 4789
		private static int nextBuff = 206;

		// Token: 0x040012B6 RID: 4790
		internal static readonly IList<ModBuff> buffs = new List<ModBuff>();

		// Token: 0x040012B7 RID: 4791
		internal static readonly IList<GlobalBuff> globalBuffs = new List<GlobalBuff>();

		// Token: 0x040012B8 RID: 4792
		private static readonly bool[] vanillaLongerExpertDebuff = new bool[206];

		// Token: 0x040012B9 RID: 4793
		private static readonly bool[] vanillaCanBeCleared = new bool[206];

		// Token: 0x040012BA RID: 4794
		private static BuffLoader.DelegateUpdatePlayer[] HookUpdatePlayer;

		// Token: 0x040012BB RID: 4795
		private static BuffLoader.DelegateUpdateNPC[] HookUpdateNPC;

		// Token: 0x040012BC RID: 4796
		private static Func<int, Player, int, int, bool>[] HookReApplyPlayer;

		// Token: 0x040012BD RID: 4797
		private static Func<int, NPC, int, int, bool>[] HookReApplyNPC;

		// Token: 0x040012BE RID: 4798
		private static BuffLoader.DelegateModifyBuffTip[] HookModifyBuffTip;

		// Token: 0x040012BF RID: 4799
		private static Action<string, List<Vector2>>[] HookCustomBuffTipSize;

		// Token: 0x040012C0 RID: 4800
		private static Action<string, SpriteBatch, int, int>[] HookDrawCustomBuffTip;

		// Token: 0x0200017C RID: 380
		// (Invoke) Token: 0x06000F4E RID: 3918
		private delegate void DelegateUpdatePlayer(int type, Player player, ref int buffIndex);

		// Token: 0x0200017D RID: 381
		// (Invoke) Token: 0x06000F52 RID: 3922
		private delegate void DelegateUpdateNPC(int type, NPC npc, ref int buffIndex);

		// Token: 0x0200017E RID: 382
		// (Invoke) Token: 0x06000F56 RID: 3926
		private delegate void DelegateModifyBuffTip(int type, ref string tip, ref int rare);
	}
}
